import {  Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { map as _map, filter as _filter } from 'lodash-es';
import { finalize } from 'rxjs/operators';
import { CreateCategoryInput, PagedCategoryDto, UpdateCategoryInput, WorkFlowDesignServiceProxy } from '@shared/service-proxies/service-proxies';

export interface IWorkFlowCategoryOnEdit {
    id?: string;
    isActive?: boolean;
    name?: string;
    order?: number;
}

@Component({
    selector: 'createOrEditFlowCategoryModal',
    templateUrl: './create-or-edit-workflow-category.component.html'
})
export class CreateOrEditFlowCategoryComponent extends AppComponentBase {
    @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    saving = false;
    workflowCategoryItem:IWorkFlowCategoryOnEdit = {order:0};
    updateCategoryInput:UpdateCategoryInput= new UpdateCategoryInput();
    constructor(
        injector: Injector,
        private _workFlowDesignServiceProxy:WorkFlowDesignServiceProxy
    ) {
        super(injector);
    }

    show(categoryItem?:PagedCategoryDto): void {
        this.workflowCategoryItem = {order:0};
        if (categoryItem){
            this.workflowCategoryItem.id = categoryItem.id;
            this.workflowCategoryItem.isActive = categoryItem.isActive;
            this.workflowCategoryItem.name = categoryItem.name;
            this.workflowCategoryItem.order = categoryItem.order;
        }

        this.modal.show();
    }


    save(): void {
        if (this.workflowCategoryItem.id){
            this.updateWorkFlowCategoryData();
        }
        else {
            this.createWorkFlowCategoryData();
        }
    }

    private createWorkFlowCategoryData():void{
        this.saving = true;
        this._workFlowDesignServiceProxy.createCategory(new CreateCategoryInput({
            name: this.workflowCategoryItem.name,
            isActive: this.workflowCategoryItem.isActive,
            order: this.workflowCategoryItem.order
        })).pipe(finalize(()=>{this.saving=false;})).subscribe(()=>{ 
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        });
    }

    private updateWorkFlowCategoryData(): void{
        this.saving = true;
        this._workFlowDesignServiceProxy.updateCategory(new UpdateCategoryInput({
            isActive:this.workflowCategoryItem.isActive,
            id:this.workflowCategoryItem.id,
            order: this.workflowCategoryItem.order,
            name: this.workflowCategoryItem.name
        })).pipe(finalize(() => { this.saving = false; })).subscribe(()=>{ 
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        })
    }

    close(): void {
        this.modal.hide();
    }
}
